package lock;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 悲观锁和乐观锁
 *
 * @author codefish
 * @version 1.0
 * @date 2022/07/30 上午 10:40
 */
public class LockStartTest {
    final List<Integer> list = new ArrayList<>();
    final ReentrantLock lock = new ReentrantLock();

    @Test
    public void testSynchronized() {
        synchronized (list) {
            list.add(555);
        }
    }

    @Test
    public void testReentrantLock() {
        lock.lock();
        try {
            list.add(555);
        } finally {
            lock.unlock();
        }
    }

    final AtomicInteger counter = new AtomicInteger(0);

    @Test
    public void test() {
        System.out.println(counter.incrementAndGet());
    }


}
